package com.itqn.servlet;

import com.alibaba.fastjson.JSONArray;
import com.itqn.pojo.Employee;
import com.itqn.pojo.PageBean;
import com.itqn.service.EmployeeService;
import com.itqn.service.Impl.EmployeeServiceImpl;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/employeeQuery")
public class EmployeeQueryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        //获取参数
        String name = request.getParameter("name");
        Integer page = Integer.valueOf(request.getParameter("page"));
        page = page == null ? 1 : page;
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        pageSize = pageSize == null ? 5 : pageSize;
        //创建业务逻辑层对象
        EmployeeService employeeService = new EmployeeServiceImpl();
        //使用业务层对象调用查询所有方法
        PageBean<Employee> queryPage = employeeService.query(name, pageSize, page);
        String str = JSONArray.toJSONString(queryPage);
        PrintWriter writer = response.getWriter();
        writer.write(str);
    }
}
